<?php

class Application_Model_dataDAO_SanPhamDAO implements Application_Model_dataDAOInf_SanPhamDAOInf {
	protected static $_instance=null;
	
	public static function getInstance(){
		if(self::$_instance==null) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	
	/**
	 * @param String $tenSanPham
	 * @param String $maSanPham
	 * @param unknown_type $idDanhMuc
	 * @param unknown_type $giaGoc
	 * @param unknown_type $giaBan
	 * @param String $moTa
	 * @param String $chucNang
	 * @param String $chucNangKhac
	 * @param String $anh
	 */
	public function insertSanPham($tenSanPham, $maSanPham, $idDanhMuc, $giaGoc, $giaBan, $moTa, $chucNang, $chucNangKhac, $anh,$tinhTrang) {
		$result=0;
		$nowDate=date("Y-m-d");
		$data=array(
			"maCuaSanPham" => $maSanPham,
			"tenSanPham" => $tenSanPham,
			"ngayTao" => $nowDate,
			"giaGoc" => $giaGoc,
			"giaBan" => $giaBan,
			"moTa" => $moTa,
			"hinhAnh" => $anh,
			"maDanhMucSanPham" => $idDanhMuc,
			"maChucNangSanPham" => $chucNang,
			"chucNangKhac" => $chucNangKhac,
			"tinhTrangSanPham" => $tinhTrang
		);
		$result=Application_Model_dataDAO_DBConnect::insertQuery('tb_sanpham',$data);
		return $result;
	}
	/**
	 * @param unknown_type $id
	 */
	public function deleteSanPham($id) {
		
	}

	/**
	 * @param unknown_type $id
	 */
	public function loadSanPham($id) {
		$sanPham=null;
		$statment="SELECT * FROM tb_sanpham WHERE maSanPham=".$id;
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		foreach ($arr as $value) {
			$sanPham=new Application_Model_bean_SanPham();
			$sanPham->setMaSanPham($value['maSanPham']);
			$sanPham->setMaCuaSanPham($value['maCuaSanPham']);
			$sanPham->setTenSanPham($value['tenSanPham']);
			$sanPham->setNgayTao($value['ngayTao']);
			$sanPham->setGiaGoc($value['giaGoc']);
			$sanPham->setGiaBan($value['giaBan']);
			$sanPham->setMoTa($value['moTa']);
			$sanPham->setHinhAnh($value['hinhAnh']);
			$sanPham->setTrangThai($value['trangThai']);
			$sanPham->setTinhTrang($value['tinhTrangSanPham']);
			$sanPham->setMaDanhMuc($value['maDanhMucSanPham']);
			$sanPham->setChucNangSanpham($value['maChucNangSanPham']);
			$sanPham->setChucNangDacBiet($value['chucNangKhac']);
		}
		//set lai chuc nang san pham là một full chuc nang
		$StringChucNang=$sanPham->getChucNangSanpham();
		$sanPham->setChucNangSanpham($this->loadDanhMucSanPhamFromString($StringChucNang));
		//set hinh anh thang mang
		$mangHinh=array();
		$StringHinh=$sanPham->getHinhAnh();
		$token=strtok($StringHinh,' ');
		$i=0;
		while ($token) {
			$mangHinh[$i]=$token;
			$token=strtok(' ');
			$i++;
		}
		$sanPham->setHinhAnh($mangHinh);
		return $sanPham;
	}

private function loadDanhMucSanPhamFromString($string){
		//Mang cac nhom chuc nang
		$result=array();
		//load het tat ca cac danh muc
		//Goi DAO
		$DAO=Application_Model_dataDAO_ChucNangSanPhamDAO::getInstance();
		$DAO instanceof Application_Model_dataDAOInf_ChucNangSanPhamDAOInf;
		//xu ly chuoi ra day chuoi so
		$mangIdItem=array();
		$i=0;
		$token=strtok($string,' ');
		while ($token) {
			$id=$token;
			$mangIdItem[$i]=(int)$id;
			$token=strtok(' ');
			$i++;
		}
		
		foreach ($mangIdItem as $value) {
			//load Item from id
			$TempItem=$DAO->getItemChucNangSanPham($value);
			$tem=$this->checkItemInListNhomChucNang($result,$TempItem);
			$result=$tem;
		}
		return $result;
	}
	
	private function checkItemInListNhomChucNang($listChucNang,$Item){
		
		$Item instanceof Application_Model_bean_ItemChucNangSanPham;
		foreach ($listChucNang as $value) {
			$value instanceof Application_Model_bean_ChucNangSanPham;
			$idNhom=$value->getId();
			if($idNhom==$Item->getIdNhomChucNang()){
				$value->pustItem($Item);
				return $listChucNang;
			}
		}
		//neu ko co thi load nhom chuc nang do len
		$idNhomChucNangOfItem=$Item->getIdNhomChucNang();
		//goi DAO
		$DAO=Application_Model_dataDAO_ChucNangSanPhamDAO::getInstance();
		$DAO instanceof Application_Model_dataDAOInf_ChucNangSanPhamDAOInf;
		$NhomChucNang=$DAO->getChucNangSanPham($idNhomChucNangOfItem);
		$NhomChucNang instanceof Application_Model_bean_ChucNangSanPham;
		//push $Item vao $NhomChucNang
		$NhomChucNang->pustItem($Item);
		//add nó vào $listChucNang
		array_push($listChucNang,$NhomChucNang);
		return $listChucNang;
	}
	
	/**
	 * @param unknown_type $limit
	 * @param unknown_type $page
	 * @param unknown_type $tenSanPham
	 * @param unknown_type $maDanhMuc
	 * @param unknown_type $giaBan
	 * @param unknown_type $maChucNang
	 */
	public function loadListSanPham($limit, $page, $tenSanPham, $maDanhMuc, $giaBan, $maChucNang) {
		$result=array();
		$offset=($page-1)*$limit;
		$statment="	SELECT * FROM tb_sanpham
					WHERE LOWER(tenSanPham) LIke LOWER('%".$tenSanPham."%')  AND
					LOWER(maDanhMucSanPham) LIKE LOWER('%".$maDanhMuc."%') AND
					LOWER(maChucNangSanPham) LIKE LOWER('%".$maChucNang."%') AND
					giaBan >= ".$giaBan."
					ORDER BY maSanPham DESC
					LIMIT ".$offset.",".$limit;
		if($maDanhMuc!=""){
			$statment="	SELECT * FROM tb_sanpham
					WHERE LOWER(tenSanPham) LIke LOWER('%".$tenSanPham."%')  AND
					LOWER(maDanhMucSanPham) LIKE LOWER('".$maDanhMuc."') AND
					LOWER(maChucNangSanPham) LIKE LOWER('%".$maChucNang."%') AND
					giaBan >= ".$giaBan."
					ORDER BY maSanPham DESC
					LIMIT ".$offset.",".$limit;
		}
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		$k=0;
		foreach ($arr as $value) {
			$sanPham=new Application_Model_bean_SanPham();
			$sanPham->setMaSanPham($value['maSanPham']);
			$sanPham->setMaCuaSanPham($value['maCuaSanPham']);
			$sanPham->setTenSanPham($value['tenSanPham']);
			$sanPham->setNgayTao($value['ngayTao']);
			$sanPham->setGiaGoc($value['giaGoc']);
			$sanPham->setGiaBan($value['giaBan']);
			$sanPham->setMoTa($value['moTa']);
			$sanPham->setHinhAnh($value['hinhAnh']);
			$sanPham->setTrangThai($value['trangThai']);
			$sanPham->setTinhTrang($value['tinhTrangSanPham']);
			$sanPham->setMaDanhMuc($value['maDanhMucSanPham']);
			$sanPham->setChucNangSanpham($value['maChucNangSanPham']);
			$sanPham->setChucNangDacBiet($value['chucNangKhac']);
			//set lai chuc nang san pham là một full chuc nang
			$StringChucNang=$sanPham->getChucNangSanpham();
			$sanPham->setChucNangSanpham($this->loadDanhMucSanPhamFromString($StringChucNang));
			//set hinh anh thang mang
			$mangHinh=array();
			$StringHinh=$sanPham->getHinhAnh();
			$token=strtok($StringHinh,' ');
			$i=0;
			while ($token) {
				$mangHinh[$i]=$token;
				$token=strtok(' ');
				$i++;
			}
			$sanPham->setHinhAnh($mangHinh);
			$result[$k]=$sanPham;
			$k++;
			
		}
		return $result;
	}
	/**
	 * @param unknown_type $limit
	 * @param unknown_type $page
	 * @param unknown_type $tenSanPham
	 * @param unknown_type $maDanhMuc
	 * @param unknown_type $giaBan
	 * @param unknown_type $maChucNang
	 */
	public function pagesNum($limit, $page, $tenSanPham, $maDanhMuc, $giaBan, $maChucNang) {
		$totalRecord=0;
		$statment="	SELECT COUNT(maSanPham) AS ma FROM tb_sanpham
					WHERE LOWER(tenSanPham) LIke LOWER('%".$tenSanPham."%')  AND
					LOWER(maDanhMucSanPham) LIKE LOWER('%".$maDanhMuc."%') AND
					LOWER(maChucNangSanPham) LIKE LOWER('%".$maChucNang."%') AND
					giaBan >= ".$giaBan;
		if($maDanhMuc!=""){
			$statment="	SELECT COUNT(maSanPham) AS ma FROM tb_sanpham
					WHERE LOWER(tenSanPham) LIke LOWER('%".$tenSanPham."%')  AND
					LOWER(maDanhMucSanPham) LIKE LOWER('".$maDanhMuc."') AND
					LOWER(maChucNangSanPham) LIKE LOWER('%".$maChucNang."%') AND
					giaBan >= ".$giaBan;
		}
		$arr=Application_Model_dataDAO_DBConnect::selectQuery($statment);
		foreach ($arr as $value) {
			$totalRecord=$value['ma'];
		}	
		$page=$totalRecord/$limit;
		$page=(int)$page;
		if($totalRecord%$limit!=0) $page++;
		return $page;
	}
	/**
	 * @param unknown_type $tenSanPham
	 * @param unknown_type $idDanhMuc
	 * @param unknown_type $moTa
	 * @param unknown_type $tinhTrang
	 */
	public function updateThongTinChung($id,$tenSanPham, $idDanhMuc, $moTa, $tinhTrang) {
		$result=false;
		try {
			$update=array("tenSanPham" => $tenSanPham,"moTa" => $moTa, "tinhTrangSanPham" => $tinhTrang , "maDanhMucSanPham" => $idDanhMuc);
			$conditions[] = 'maSanPham='.$id;
		
			$db=Application_Model_dataDAO_DBConnect::conn();
			$value=$db->update('tb_sanpham',$update,$conditions);
			if($value==1) $result=true;
			$db->closeConnection();
		} catch (Zend_Db_Exception  $e) {
			echo $e->getMessage();
		}
		
		return $result;
	}




	
	

	
	
	
	
}

?>